package org.tabcap.workspace.gui.output;

import java.awt.BorderLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

public class TCOutput extends JPanel
{
	private static final long serialVersionUID = -119456594598973685L;
	
	private JTable table;
	public JTable table() { return table; }
	
	private TCTableModel tableModel;
	public String[][] data() { return tableModel.data(); }
	public void setData(String[][] data) { tableModel.setData(data); }
	
	public TCOutput()
	{
		setLayout(new BorderLayout());
		
		table = new JTable(tableModel = new TCTableModel());
		
		JScrollPane scrollpane = new JScrollPane(table, 
				JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, 
				JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
		
		add(scrollpane, BorderLayout.CENTER);
	}
	
	private class TCTableModel extends AbstractTableModel
	{
		private static final long serialVersionUID = 8543160410837161602L;
		
		private String[][] data;
		public String[][] data() { return data; }
		public void setData(String[][] data)
		{
			this.data = data;
			for(int i = 0; i < data.length; i++)
				for(int j = 0; j < data[i].length; j++)
					if(data[i][j] == null)
						data[i][j] = "";
		}
		
		public TCTableModel() { this(10, 10); }
		public TCTableModel(int rows, int cols) { this(new String[rows][cols]); }
		public TCTableModel(String[][] data) { setData(data); }

		@Override public int getRowCount() { return data.length; }
		@Override public int getColumnCount() { return data[0].length; }
		@Override public Object getValueAt(int row, int col) { return data[row][col]; }
	}
}